package com.stox.client.ui.javafx.core;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.input.DataFormat;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;

import com.stox.client.ui.core.model.intf.HasNavigable;
import com.stox.client.ui.core.model.intf.Navigable;
import com.stox.shared.core.model.intf.HasScrip;

public class ScripDragEventHandler implements EventHandler<MouseEvent> {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void handle(MouseEvent event) {
		Node node = (Node) event.getSource();
		if (MouseEvent.DRAG_DETECTED.equals(event.getEventType()) && node instanceof HasNavigable) {
			HasNavigable<HasScrip> hasNavigable = (HasNavigable)node;
			Navigable<HasScrip> navigable = hasNavigable.getNavigable();
			if(null != navigable){
				List<HasScrip> hasScrips = navigable.currentAllSelection();
				if(null != hasScrips && !hasScrips.isEmpty()){
					Dragboard db = node.startDragAndDrop(TransferMode.ANY);

					/* As we have to put something in order to get the drag event started,
					 * a dummy string is put in content*/
					Map<DataFormat, Object> map = new HashMap<DataFormat,Object>(1);
					map.put(DataFormat.PLAIN_TEXT, "Dummy Value - Not really needed");
					db.setContent(map);
					event.consume();
				}
			}
			
			
		}
	}

}
